//Mark O Meara
//08332240
//CS2014 - Assignment2
//Due: 31/03/10

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class Assignment2_B {
	public static void main(String[] args) {
		try {
			// Get xml from "Assignment2_OutputA.xml" (Where last app's xml was saved)
			File file = new File("Assignment2_OutputA.xml");
			// Parse it into a DOM “Document”
			Document doc = null;
			org.jdom.input.SAXBuilder builder = new org.jdom.input.SAXBuilder();
			doc = builder.build(file);
			Element root = doc.getRootElement();
			//Call method to convert all year attributes to elements
			ElementMethods.yearAttributeToElement(root);
			/*Output stuff*/
			XMLOutputter outputter = new XMLOutputter();
			outputter.setFormat(Format.getPrettyFormat().setIndent("\t"));
			//Set file name for saving
			FileWriter Xmlfile = new FileWriter("Assignment2_OutputB.xml", false);
			//Ouput xml to system.out
			outputter.output(doc, System.out);
			//Save new xml file in "Assignment2_OutputB.xml"
			outputter.output(doc, Xmlfile);
			Xmlfile.flush();
			Xmlfile.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (JDOMException e) {
			e.printStackTrace();
		}
	}
}